package router

import (
	"bluebell/controller"
	_ "bluebell/docs" // 千万不要忘了导入docs
	"bluebell/logger"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"
)

func SetupRouter() *gin.Engine {
	r := gin.New()                                      // 新建gin实例(不含默认中间件: Logger(), Recovery())
	r.Use(logger.GinLogger(), logger.GinRecovery(true)) // 使用自定义日志中间件

	// gin-swagger接口文档控制器, 简单使用:r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))
	// 更改"NAME_OF_ENV_VARIABLE",将为禁用状态,返回404
	r.GET("/swagger/*any", gs.DisablingWrapHandler(swaggerFiles.Handler, "NAME_OF_ENV_VARIABLE"))

	// 注册业务路由(不加密,不需要登录)
	rApi := r.Group("/api")
	{
		rApi.POST("/signUp", controller.SignUpHandler) // 注册
		rApi.POST("/login", controller.LoginHandler)   // 登录
		rApi.GET("/vote", controller.PostListByFactorHandler)
		rApi.GET("/community/vote", controller.CommunityPostListHandler)
		rApi.GET("/houses/conditions/:id", controller.ConditionsListHandler)
		rApi.GET("/community", controller.CommunityHandler)
		rApi.POST("/upload", controller.UploadHandler)
	}

	// 注册业务路由组(不加密,需要登录)
	rJwt := r.Group("/api/jwt")
	rJwt.Use(JWTMiddleware())
	{
		rJwt.GET("/user", controller.UserInfoHandler)
		rJwt.GET("/community/:id", controller.CommunityDetailHandler)
		rJwt.POST("/post", controller.CreatePostHandler)
		rJwt.GET("/post/:id", controller.PostDetailHandler)
		rJwt.GET("/post", controller.PostListHandler)
		rJwt.POST("/vote", controller.PostVoteHandler)
	}

	// 路由不存在返回信息
	r.NoRoute(func(ctx *gin.Context) {
		controller.ResponseError(ctx, 400, "访问的控制器不存在")
	})
	return r
}
